﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using LiSAClient.Core;
using LiSAClient.Core.Interfaces;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;
using LiSAClient.LiSAService;

namespace LiSAClient.WpfClient.ViewModel.Projects.Edit
{
    class EditMembersViewModel : ContentViewModel, ICommitable
    {
        #region Observable Objects
        public ICommand Commit { get; private set; }

        public ICommand AddUser { get; private set; }

        public ICommand DisableEmployee { get; private set; }

        public ICommand ChangeLead { get; private set; }

        private List<User> usersInSystem;
        public List<User> UsersInSystem
        {
            get
            {
                return this.usersInSystem;
            }

            set
            {
                this.Set(() => this.UsersInSystem, ref this.usersInSystem, value);
            }
        }

        private User selectedUser;
        public User SelectedUser
        {
            get
            {
                return this.selectedUser;
            }

            set
            {
                this.Set(() => this.SelectedUser, ref this.selectedUser, value);
            }
        }

        private User newUser;
        public User NewUser
        {
            get
            {
                return this.newUser;
            }

            set
            {
                this.Set(() => this.NewUser, ref this.newUser, value);
            }
        }

        private Project project;
        public Project Project
        {
            get
            {
                return this.project;
            }

            set
            {
                this.Set(() => this.Project, ref this.project, value);
            }
        }
        #endregion
        #region Initializers
        public EditMembersViewModel(Project proj)
        {
            this.Project = proj;
            this.Commit = new RelayCommand(UpdateProject);
            this.AddUser = new RelayCommand(DoAddUser);
            this.DisableEmployee = new RelayCommand(DoDisableEmployee);
            this.ChangeLead = new RelayCommand(DoChangeLead);
            this.SelectedUser = new User();
            this.UsersInSystem = new List<User>();
        }

        public override void OnPresented()
        {
            this.Presenter.StartWaiting("Fetching users...");
            Task.Factory.StartNew(
                () =>
                {
                    if(this.UsersInSystem == null) this.UsersInSystem = new List<User>();
                    var theusers = ServiceFactory.Instance.Get<UserService>().GetAllUsers();
                    foreach (var aUSer in theusers)
                    {
                        this.UsersInSystem.Add(aUSer);
                    }

                    // No point in listing the current user since it will be added upon creation anyway by the service
                    if (UsersInSystem.Any(x => x.UserName == AppContext.CurrentContext.User.UserName))
                    {
                        UsersInSystem.Remove(
                            UsersInSystem.SingleOrDefault(x => x.UserName == AppContext.CurrentContext.User.UserName));
                    }
                    this.Presenter.FinishedWaiting();
                });
           
        }
        #endregion

        #region methods
        /// <summary>
        /// Move to the next step in the wizard
        /// </summary>
        public void DoAddUser()
        {
            if (this.SelectedUser != null && !string.IsNullOrWhiteSpace(this.SelectedUser.FullName))
            {
                var newUsers = new List<User>(this.Project.Users) {this.SelectedUser};
                this.Project.Users = newUsers;
                this.UsersInSystem.Remove(this.SelectedUser);
            }
        }

        /// <summary>
        /// Save the project and close the window
        /// </summary>
        public void UpdateProject()
        {
            MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Update Project?", "Update Confirmation", System.Windows.MessageBoxButton.YesNo);
            if (messageBoxResult == MessageBoxResult.Yes)
            {
                ServiceFactory.Instance.Get<ProjectService>().UpdateProject(this.Project);

                // Close the window
                if (this.Presenter != null)
                {
                    this.Presenter.SendClosingRequest();
                }
            }
        }

        public void DoDisableEmployee()
        {
            // TODO: Consider how to handle this. Should it even be allowed?
        }

        /// <summary>
        /// Changes the project lead to be the selected user
        /// </summary>
        public void DoChangeLead()
        {
            this.Project.ProjectLead = this.SelectedUser.ID;
        }
        #endregion
    }
}
